package com.hc.utils;

import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
public class MyDateFormat implements Converter<String, Date> {
    private String pattern1="yyyy-MM-dd";
    private String pattern2="yyyy/MM/dd";
    @SneakyThrows
    @Override
    public Date convert(String str) {
        try {
            SimpleDateFormat format=new SimpleDateFormat(pattern1);
            return format.parse(str);
        } catch (ParseException e) {
//            e.printStackTrace();
            SimpleDateFormat format=new SimpleDateFormat(pattern2);
            try {
                return format.parse(str);
            } catch (ParseException parseException) {
                parseException.printStackTrace();
                throw new Exception("日期转换失败");
            }
        }
    }
}
